﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ConfigService;
using System.IO.Ports;
using LogLib;
using ComLib.ComFun;

namespace LaserMeasure
{
    /// <summary>
    /// 距离传感器参数配置选项
    /// </summary>
    public class LaserOption : IniConfigBase
    {
        public LaserOption()
            : base()
        {
        }

        /// <summary>
        /// 传感器连接的串口号
        /// </summary>
        private string _comNo = "COM8";
        public string ComNo
        {
            get { return _comNo; }
            set { _comNo = value; }
        }

        /// <summary>
        /// 传感器连接串口波特率
        /// </summary>
        private int _baudRate = 38400;
        public int BaudRate
        {
            get { return _baudRate; }
            set { _baudRate = value; }
        }

        /// <summary>
        /// 传感器连接串口数据位
        /// </summary>
        private int _dataBit = 8;
        public int DataBit
        {
            get { return _dataBit; }
            set { _dataBit = value; }
        }

        /// <summary>
        /// 传感器连接串口停止位
        /// </summary>
        private StopBits _stopBit = StopBits.One;
        public StopBits StopBit
        {
            get { return _stopBit; }
            set { _stopBit = value; }
        }

         /// <summary>
        /// 传感器连接串口奇偶校验位
        /// </summary>
        private Parity _parity = Parity.None;
        public Parity Parity
        {
            get { return _parity; }
            set { _parity = value; }
        }

         /// <summary>
        /// 传感器连接串口通信超时时间
        /// </summary>
        private int _readTimeout = 3000;
        public int ReadTimeout
        {
            get { return _readTimeout; }
            set { _readTimeout = value; }
        }

        /// <summary>
        /// 连续扫描的间隔时间(ms)
        /// </summary>
        private int _scanInterval = 500;
        public int ScanInterval
        {
            get { return _scanInterval; }
            set { _scanInterval = value; }
        }

        /// <summary>
        /// 测量范围（mm）
        /// </summary>
        private int _range = 3000;
        public int Range
        {
            get { return _range; }
            set { _range = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("ComNo", _comNo);
            SaveField("BaudRate", _baudRate);
            SaveField("DataBit", _dataBit);
            SaveField("StopBit", _stopBit);
            SaveField("Parity", _parity);
            SaveField("ReadTimeout", _readTimeout);
            SaveField("ScanInterval", _scanInterval);
            SaveField("Range", Range);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _comNo = LoadStringValue("ComNo");
                _baudRate = LoadInt32Value("BaudRate");
                _dataBit = LoadInt32Value("DataBit");
                _stopBit = LoadEnumValue<StopBits>("StopBit");               
                _parity = LoadEnumValue<Parity>("Parity");
                _readTimeout = LoadInt32Value("ReadTimeout");
                _scanInterval = LoadInt32Value("ScanInterval");
                _range = LoadInt32Value("Range");
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {          
            StringBuilder builder = new StringBuilder("距离传感器选项配置: ");
            FillFieldValueStringDelegate FillString =
                (string field, object val) =>
                {
                    builder.Append("[");
                    builder.Append(field);
                    builder.Append("]: ");
                    builder.Append(val.ToString() + "; ");
                };
            FillString("串口", _comNo);
            FillString("波特率", _baudRate);
            FillString("停止位", _stopBit);
            FillString("数据位", _dataBit);
            FillString("奇偶校验位", _parity);
            FillString("超时时间", _readTimeout);
            FillString("连续扫描的间隔时间", _scanInterval);
            FillString("量程", _range);
            return builder.ToString();
        }
    }
}
